//
//  AWMessageResolver.m
//  AtWork2
//
//  Created by lucas on 15/6/7.
//  Copyright (c) 2015年 Foreveross. All rights reserved.
//

#import "AWMessageResolver.h"
#import "AWChatMessageResolver.h"
#import "AWNoticeMessageResolver.h"
#import "AWMessage.h"
#import "AWCmdMessage.h"
#import "AWChatMessage.h"
#import "AWNoticeMessage.h"
#import "AWAckMessage.h"
#import "ATWUtil.h"

@implementation AWMessageResolver

+ (AWMessage *)resolveIfPossibleWithDictionary:(NSDictionary *)message {
    if (!message[AWMessageKType]) { // TODO：暂时这么处理。
        return nil;
    }
    AWMessageType type = parseWithMessageType(message[AWMessageKType]);
    
    if (type == AWMessageTypeNotice && message[AWMessageKBody][AWMessageKDeviceId] && [message[AWMessageKBody][AWMessageKDeviceId] isEqualToString:[ATWUtil deviceId]]) {
        return nil;
    }
    switch (type) {
        case AWMessageTypeCmd:
            return [self resolveToCmdMessageWithDictionary:message];
        case AWMessageTypeServeNo:
        case AWMessageTypeChat:
            return [self resolveToChatMessageWithDictionary:message];
        case AWMessageTypeNotice:
            return [self resolveToNoticeMessageWithDictionary:message];
        case AWMessageTypeAck:
            return [self resolveToAckMessageWithDictionary:message];
        case AWMessageTypeVoip: //TODO: 这些类型目前采用默认的解析方式。
        case AWMessageTypeReq:
        case AWMessageTypeResp:
        case AWMessageTypeCustom:
            return nil;
    }
}

+ (AWMessage *)resolveToCmdMessageWithDictionary:(NSDictionary *)message {
    return [AWCmdMessage messageWithDictionary:message];
}

+ (AWMessage *)resolveToChatMessageWithDictionary:(NSDictionary *)message {
    return [AWChatMessageResolver resolveIfPossibleWithDictionary:message];
}

+ (AWMessage *)resolveToNoticeMessageWithDictionary:(NSDictionary *)message {
    return [AWNoticeMessageResolver resolveIfPossibleWithDictionary:message];
}

+ (AWMessage *)resolveToAckMessageWithDictionary:(NSDictionary *)message {
    return [AWAckMessage messageWithDictionary:message];
}

@end
